//
//  ChoseCityViewController.m
//  League
//
//  Created by JackChen on 2018/2/20.
//  Copyright © 2018年 ciyuanjuzhen. All rights reserved.
//

#import "ChoseCityViewController.h"
#import "ProdileModel.h"

@interface ChoseCityViewController ()<UIPickerViewDelegate,  UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

@end

@implementation ChoseCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    if (self.isProfile == YES) {
        for (int i = 0; i<self.dataSource.count; i++) {
            ProdileModel *model = self.dataSource[i];
            if ([model._id isEqualToString:[ProfileManager shareManager].location]) {
                [self.pickerView selectRow:i inComponent:0 animated:YES];
            }
        }
    } else {
        for (int i = 0; i<self.dataSource.count; i++) {
            ProdileModel *model = self.dataSource[i];
            if ([model._id isEqualToString:[ProfileManager shareManager].preferLocation]) {
                [self.pickerView selectRow:i inComponent:0 animated:YES];
            }
        }
    }
}

- (IBAction)cancleAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (IBAction)sureAction:(UIButton *)sender {
    self.selectBlock();
    [self dismissViewControllerAnimated:NO completion:nil];
}


#pragma mark -- UIPickerViewDelegate and UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.dataSource.count;
}

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    ProdileModel *model = self.dataSource[row];
    NSString *lan = [InternationalControl userLanguage];
    if([lan isEqualToString:@"en"]){//判断当前的语言，进行改变
        return model.categoryDesc;
    } else {
        return model.categoryName;
    }
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"%ld", component);
    ProdileModel *model = self.dataSource[row];
    if (self.isProfile == YES) {
        [ProfileManager shareManager].location = model._id;
    } else {
        [ProfileManager shareManager].preferLocation = model._id;
    }
}


@end
